// formats格式包括
// 1. Y-m-d
// 2. Y-m-d H:i:s
// 3. Y年m月d日
// 4. Y年m月d日 H时i分
export function dateFormat(timestamp, formats) {
	formats = formats || 'y-m-d'

	var zero = function(value) {
		if (value < 10) {
			return '0' + value
		}
		return value
	}

	var myDate = timestamp ? new Date(timestamp) : new Date()

	var year = myDate.getFullYear()
	var month = zero(myDate.getMonth() + 1)
	var day = zero(myDate.getDate())

	var hour = zero(myDate.getHours())
	var minite = zero(myDate.getMinutes())
	var second = zero(myDate.getSeconds())

	if (formats.indexOf('yyyy') > -1) {
		return formats.replace(/yyyy|MM|dd|HH|mm|ss/gi, function(matches) {
			return {
				yyyy: year,
				MM: month,
				dd: day,
				HH: hour,
				mm: minite,
				ss: second,
			}[matches]
		})
	} else {
		return formats.replace(/y|m|d|h|i|s/gi, function(matches) {
			return {
				y: year,
				m: month,
				d: day,
				h: hour,
				i: minite,
				s: second,
			}[matches]
		})
	}
}

//取字符长度，中文字符 按2个长度计算
export function strLen(str) {
	var len = 0
	for (let i = 0; i < str.length; i++) {
		if ((str.charCodeAt(i) & 0xff00) != 0) {
			len++
		}
		len++
	}
	return len
}

//复杂数据拷贝
export function deepCopy(obj) {
	//复杂数据复制
	var result = Array.isArray(obj) ? [] : {}
	for (var key in obj) {
		//		if(obj.hasOwnProperty(key)) {
		if (Object.prototype.hasOwnProperty.call(obj, key)) {
			if (obj[key] === null) {
				result[key] = null
			} else if (typeof obj[key] === 'object') {
				result[key] = deepCopy(obj[key]) //递归复制
			} else {
				result[key] = obj[key]
			}
		}
	}
	return result
}

export function isEqual(a, b) {
	console.log(a)
	console.log(b)
	let res = true
	if (Array.isArray(a) == Array.isArray(b)) {
		if (Array.isArray(a) && Array.isArray(b)) {
			if (!arrEqual(a, b)) {
				res = false
			}
		} else {
			if (!objEqual(a, b)) {
				res = false
			}
		}
	} else {
		res = false
	}
	return res
}

function arrEqual(a, b) {
	if (a.length != b.length) {
		return false
	} else {
		for (let i = 0; i < a.length; i++) {
			if (Array.isArray(a[i]) == Array.isArray(b[i])) {
				//判断2个都是数组 或者 都不是数组
				if (Array.isArray(a[i]) && Array.isArray(b[i])) {
					//都是数组  2个属性取出来的值进行判断 如果都是数组按数组走
					if (!arrEqual(a[i], b[i])) {
						return false
					}
				} else {
					if (typeof a[i] === 'object' && typeof b[i] === 'object') {
						//都是对象，按对象走
						if (!objEqual(a[i], b[i])) {
							return false
						}
					} else {
						if (typeof a[i] !== 'object' && typeof b[i] !== 'object') {
							//都不是对象判断值是否相同
							if (a[i] !== b[i]) {
								console.log('数组内容不相等 ')
								console.log(a[i])
								console.log(b[i])
								return false
							}
						} else {
							console.log('数组内容 一个是对象，一个是值')
							console.log(a[i])
							console.log(b[i])
							return false //一个是对象，一个是值
						}
					}
				}
			} else {
				console.log('数组内容a 属性b没有')
				console.log(a[i])
				console.log(b[i])
				return false //a 属性b没有
			}
		}
	}
	return true
}

function objEqual(a, b) {
	if (a == null && b == null) {
		//如果传进来的 a b 是null  或者 一个是null  一个不是null 进行判断
		return true
	} else {
		if (a == null && b != null) {
			console.log('对象 a null， 对象b')
			console.log(a)
			console.log(b)
			return false
		}
		if (a != null && b == null) {
			console.log('对象 a， 对象b null')
			console.log(a)
			console.log(b)
			return false
		}
	}
	delete a.__ob__
	delete b.__ob__
	let aProps = Object.getOwnPropertyNames(a)
	let bProps = Object.getOwnPropertyNames(b)
	// 判断两个对象键值数组长度是否一致，不一致返回false
	if (aProps.length !== bProps.length) {
		return false
	} else {
		for (let key in a) {
			//遍历a属性
			if (Object.prototype.hasOwnProperty.call(b, key)) {
				//判断b对象是否存在属性
				if (Array.isArray(a[key]) == Array.isArray(b[key])) {
					//a,b 都是数组  或者 都不是数组
					if (Array.isArray(a[key]) && Array.isArray(b[key])) {
						//都是数组  2个属性取出来的值进行判断 如果都是数组按数组走
						if (!arrEqual(a[key], b[key])) {
							return false
						}
					} else {
						if (typeof a[key] === 'object' && typeof b[key] === 'object') {
							//都是对象，按对象走
							if (!objEqual(a[key], b[key])) {
								return false
							}
						} else {
							if (typeof a[key] !== 'object' && typeof b[key] !== 'object') {
								//都不是对象判断值是否相同

								if (a[key] !== b[key]) {
									console.log(' 对象 a != 对象b')
									console.log(a[key])
									console.log(b[key])
									return false
								}
							} else {
								console.log(' 对象 a， 对象b是值')
								console.log(a[key])
								console.log(b[key])
								return false //一个是对象，一个是值
							}
						}
					}
				} else {
					return false //a 属性b没有
				}
			} else {
				return false //a 属性b没有
			}
		}
	}
	return true
}

// 首字母大小
export function titleCase(str) {
	return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
}

// 下划转驼峰
export function camelCase(str) {
	return str.replace(/-[a-z]/g, str1 => str1.substr(-1).toUpperCase())
}

export function helloTime() {
	var now = new Date()
	var hour = now.getHours()
	if (hour < 6) {
		return '凌晨好'
	} else if (hour < 9) {
		return '早上好'
	} else if (hour < 12) {
		return '上午好'
	} else if (hour < 14) {
		return '中午好'
	} else if (hour < 17) {
		return '下午好'
	} else if (hour < 19) {
		return '傍晚好'
	} else if (hour < 22) {
		return '晚上好'
	} else {
		return '夜里好'
	}
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time) {
	if (('' + time).length === 10) {
		time = parseInt(time) * 1000
	} else {
		time = +time
	}
	const d = new Date(time)
	const now = Date.now()

	const diff = (now - d) / 1000

	if (diff < 30) {
		return '刚刚'
	} else if (diff < 3600) {
		// less 1 hour
		return Math.ceil(diff / 60) + '分钟前'
	} else if (diff < 3600 * 24) {
		return Math.ceil(diff / 3600) + '小时前'
	} else if (diff < 3600 * 24 * 7) {
		return '1天前'
	} else if (diff < 3600 * 24 * 30) {
		return '1周前'
	} else if (diff < 3600 * 24 * 365) {
		return '1月前'
	} else {
		return '1年前'
	}
}

let icons = [
	{
		path: '/definition',
		icon: 'iconxitongpingtai-tongyongbaobiao',
		name: 'definition',
	}, //通用报表
	{
		path: '/message',
		icon: 'iconxitongpingtai-xiaoxizhongxin-zong',
		name: 'message',
	}, //消息中心
	{
		path: '/listStructure',
		icon: 'iconxitongpingtai-biaojiegou',
		name: 'listStructure',
	}, //表结构
	{
		path: '/organization/index',
		icon: 'iconxitongpingtai-zuzhiquanxianzhongxin-zuzhijigouguanli',
		name: 'organization',
	}, //组织权限
	{
		path: '/menuManage',
		icon: 'iconxitongpingtai-caidanguanli',
		name: 'menuManage',
	},
	{
		path: '/remindManage',
		icon: 'iconxitongpingtai-yujingguanli',
		name: 'remindManage',
	},
	{
		path: '/cellEdit',
		icon: 'iconxitongpingtai-biaodanguanli',
		name: 'cellEdit',
	},
	{
		path: '/flowEdit',
		icon: 'iconxitongpingtai-liuchengguanli',
		name: 'flowEdit',
	},
	{
		path: '/businessEdit',
		icon: 'iconxitongpingtai-yewumoxing',
		name: 'business',
	},
]
export function geticon(val, type = 1) {
	let iconName = ''

	icons.forEach((item, index) => {
		if (type == 1) {
			if (item.path == val) {
				iconName = item.icon
			}
		} else {
			if (item.name == val) {
				iconName = item.icon
			}
		}
	})
	return iconName
}

export function digitUppercase(n) {
	var fraction = ['角', '分']
	var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
	var unit = [
		['元', '万', '亿'],
		['', '拾', '佰', '仟'],
	]
	var head = n < 0 ? '欠' : ''
	n = Math.abs(n)
	var s = ''
	for (var i = 0; i < fraction.length; i++) {
		s += (
			digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]
		).replace(/零./, '')
	}
	s = s || '整'
	n = Math.floor(n)
	for (var i = 0; i < unit[0].length && n > 0; i++) {
		var p = ''
		for (var j = 0; j < unit[1].length && n > 0; j++) {
			p = digit[n % 10] + unit[1][j] + p
			n = Math.floor(n / 10)
		}
		s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
	}
	return (
		head +
		s
			.replace(/(零.)*零元/, '元')
			.replace(/(零.)+/g, '零')
			.replace(/^整$/, '零元整')
	)
}

export function round(num, n) {
	let times = Math.pow(10, n)
	let des = num * times + 0.5
	des = parseInt(des, 10) / times
	return des
}
export function filterObj(obj) {
	if (!(typeof obj === 'object')) {
		return
	}
	for (var key in obj) {
		if (obj.hasOwnProperty(key)) {
			if (obj[key] == null || obj[key] === undefined || obj[key] === '') {
				delete obj[key]
			}
			// else if (Array.isArray(obj[key])) {
			//   const arr = obj[key];
			//   if (arr.length === 2) {
			//     obj[key + '_begin'] = arr[0];
			//     obj[key + '_end'] = arr[1];
			//     delete obj[key];
			//   }
			// }
		}
	}
	return obj
}
/**
 * 防抖
 * @param {*} fn
 * @param {*} delay
 * @returns
 */
export function debounce(fn, delay) {
	var timer = null
	return function() {
		if (timer) {
			clearTimeout(timer)
		}
		timer = setTimeout(() => {
			fn.apply(this, arguments) //this和参数
			timer = null
		}, delay)
	}
}
/**
 * 节流
 * @param {*} fn
 * @param {*} delay
 * @returns
 */
export function throttle(fn, delay) {
	var timer = null
	return function() {
		if (timer) {
			return
		}
		timer = setTimeout(() => {
			fn.apply(this, arguments) //把this和参数传递过去
			timer = null
		}, delay)
	}
}
